/*
 * @brief: 计时器
 * @date: 2015.09.06
 */
 #include "string.h"
#include "timer.h"
#include <assert.h>
#include <string.h>
#include "stdio.h"


void timer_init(CHECK_TIMER *timer)
{
    assert(timer != NULL);
    timer->exec = FALSE;
    timer->acc_time = 0;
    timer->set_time = 0;
    timer->timeout_func = NULL;
}

void timer_start(CHECK_TIMER *timer, short acc_time, short set_time, TIMEOUT_FUNC pFunc)
{
    assert(timer != NULL);
    timer->exec = TRUE;
    timer->acc_time = acc_time;
    timer->set_time = set_time;
    timer->timeout_func = pFunc;
}

int timer_exec(MH_UDP *pudp, CHECK_TIMER *timer, void *user_data)
{
    assert(timer != NULL);
    if (timer->exec)
    {
        timer->acc_time += 10;
        if (timer->acc_time >= timer->set_time)
        {
            timer->exec = FALSE;
            timer->acc_time = 0;
            if (timer->timeout_func != NULL)
            {
                return timer->timeout_func(pudp, user_data);
            }
        }
    }

    return 1;
}

int timer_timeout(CHECK_TIMER *timer)
{
    assert(timer != NULL);
    return (timer->acc_time >= timer->set_time);
}

void timer_stop(CHECK_TIMER *timer)
{
    assert(timer != NULL);
    timer->exec = FALSE;
    timer->acc_time = 0;
    timer->set_time = 0;
}
